package com.ruoyi.patrol.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.patrol.domain.PtAppVersion;
import com.ruoyi.patrol.mapper.PtAppVersionMapper;
import com.ruoyi.patrol.service.PtAppVersionService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (PtAppVersion)表服务实现类
 *
 * @author makejava
 * @since 2023-02-23 10:22:21
 */
@Service("ptAppVersionService")
public class PtAppVersionServiceImpl implements PtAppVersionService {
    @Resource
    private PtAppVersionMapper ptAppVersionMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public PtAppVersion queryById(Integer id) {
        return this.ptAppVersionMapper.queryById(id);
    }

    /**
     * 查询单条数据
     *
     * @param ptAppVersion 实例对象
     * @return 实例对象
     */
    @Override
    public PtAppVersion queryOne(PtAppVersion ptAppVersion) {
        return this.ptAppVersionMapper.queryOne(ptAppVersion);
    }

    /**
     * 分页查询
     *
     * @param ptAppVersion 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<PtAppVersion> queryByPage(PageRequest pageRequest) {
        long total = this.ptAppVersionMapper.count();
        return new PageImpl<>(this.ptAppVersionMapper.queryAllByLimit(pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param ptAppVersion 实例对象
     * @return 实例对象
     */
    @Override
    public PtAppVersion insert(PtAppVersion ptAppVersion) {

        this.ptAppVersionMapper.insert(ptAppVersion);
        return ptAppVersion;
    }

    /**
     * 修改数据
     *
     * @param ptAppVersion 实例对象
     * @return 实例对象
     */
    @Override
    public PtAppVersion update(PtAppVersion ptAppVersion) {
        this.ptAppVersionMapper.update(ptAppVersion);
        return this.queryById(ptAppVersion.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.ptAppVersionMapper.deleteById(id) > 0;
    }

    @Override
    public boolean setNew(Integer id) {
        int count = this.ptAppVersionMapper.checkHasNewVersion();
        if (count>0){
            return false;
        }
        PtAppVersion ptAppVersion = new PtAppVersion();
        ptAppVersion.setId(id);
        ptAppVersion.setLatest(1);
        ptAppVersion.setUpdateTime(DateUtils.getNowDate());
        this.ptAppVersionMapper.update(ptAppVersion);
        return true;
    }

    @Override
    public boolean setOld(Integer id) {
        PtAppVersion ptAppVersion = new PtAppVersion();
        ptAppVersion.setId(id);
        ptAppVersion.setLatest(0);
        ptAppVersion.setUpdateTime(DateUtils.getNowDate());
        this.ptAppVersionMapper.update(ptAppVersion);
        return true;
    }
}
